var val_work_id=$("#workOrderId").val()
var listWork={
    workOrderId:val_work_id
}
$(function () {
    ajaxForm()
});

function ajaxForm() {
    workAjaxPost('/workOrder/workOrderReplyList',listWork,'application/x-www-form-urlencoded; charset=UTF-8').then((res)=>{
        var htmlList=''
        if(res.code=='000000'){
            for(let i=0,len=res.data.length;i<len;i++){
                htmlList+=`<div class="follow-text">
                        <h5>回复人：<span>${res.data[i].userName}</span>/<em>${res.data[i].createTm}</em></h5>
                        <p>回复内容：${res.data[i].replyContent}</p>
                    </div>`
            }
            $("#followList").html(htmlList)
            // htmlList
        }
    })
}

//点击弹框的回复 // 回复工单 /workOrder/addWorkOrderReply
function publish() {
    $('.work-informat').bootstrapValidator({
        fields: {
            followContent: {
                validators: {
                    notEmpty: {
                        message: '回复信息不能为空'
                    },
                    stringLength: {
                        max: 200,
                        message: '回复信息长度必须200之间'
                    }
                }
            },


        },
    })
    $(".work-informat").bootstrapValidator('validate');//提交验证
    if (!$(".work-informat").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码

        return;
    }
    var dataWork={
        workOrderId:val_work_id,
        replyContent:$("#followContent").val()
    }

    workAjaxPost('/workOrder/addWorkOrderReply',JSON.stringify(dataWork),'application/json;charset=utf-8').then((res)=>{
        if(res.code=='000000'){

            ajaxForm()
        }
    })
    // $("#followContent").val('')
    $(".content-reply").find('form')[0].reset()
    //
    $(".t_h i").html($("#followContent").val().length)
    if($(".work-informat").data('bootstrapValidator')!=undefined){
        // $(".work-informat").data('bootstrapValidator').destroy();
        // $('.work-informat').data('bootstrapValidator')
        //     .updateStatus('followContent', 'NOT_VALIDATED',null)
        //     .validateField('followContent');
        // $('.work-informat').data('bootstrapValidator')
        //     .validateField('followContent');
        $(".follow").load(location.href + " .follow")
        $('.work-informat').data('bootstrapValidator', null);
    }

}

//回复数字变化
function changeReply() {
    $(".t_h i").html($("#followContent").val().length)
}

function workAjaxPost(url,data,contentType) {
    // contentType: "application/json;charset=utf-8",
    var promises=new Promise((resolve, reject)=>{
        $.ajax({
            type: 'post',
            url: url,
            data: data,
            contentType:contentType,
            dataType: 'json',
            async:false,
            success: function (res) {
                resolve(res)
                if(res.code!='000000'){
                    $.alert(res.message)
                }
            },
            error: function (res) {
                reject(res)
                $.alert(res.message)
            }
        });
    })

    promises.catch(function(error) {
        $.alert(error.message)
    });

    return promises

}